<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="guardian">
        <el-input
          v-model="queryParams.guardian"
          placeholder="请输入监护人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="guardianPhone">
        <el-input
          v-model="queryParams.guardianPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:student:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['edu:student:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="guardian" />
      <el-table-column label="联系人电话" align="center" prop="guardianPhone" />
      <el-table-column label="学习目标" align="center" prop="studyTarget" />
      <el-table-column label="积分" align="center" prop="learningPoints" />
      <el-table-column label="备注" align="center" prop="remark"  show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:student:edit']"
          >修改</el-button>
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-notebook-2"-->
<!--              @click="handleWrongQuestionBook(scope.row)"-->
<!--              v-hasPermi="['edu:student:edit']"-->
<!--            >错题本</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-refresh"-->
<!--            @click="handleRestPwd(scope.row)"-->
<!--          >重置密码</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['edu:student:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日" prop="birthday">
              <el-date-picker clearable
                              v-model="form.birthday"
                              type="date" style="width: 100%"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学习目标" prop="studyTarget">
              <el-input v-model="form.studyTarget" placeholder="请输入学习目标" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="guardian">
              <el-input v-model="form.guardian" placeholder="请输入监护人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="guardianPhone">
              <el-input v-model="form.guardianPhone" placeholder="请输入监护人电话" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="有无过敏" prop="foodAllergiesFlag">-->
<!--              <el-radio-group v-model="form.foodAllergiesFlag">-->
<!--                <el-radio v-for="dict in dict.type.sys_have_not" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="过敏源" prop="foodAllergens">-->
<!--              <el-input v-model="form.foodAllergens" :disabled="!form.isFoodAllergies" placeholder="请输入食物过敏源" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="有无疾病" prop="diseaseFlag">-->
<!--              <el-radio-group v-model="form.diseaseFlag">-->
<!--                <el-radio v-for="dict in dict.type.sys_have_not" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="疾病史" prop="diseaseName">-->
<!--              <el-input v-model="form.diseaseName" :disabled="!form.isDisease" placeholder="请输入疾病史" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="重置密码" :visible.sync="restPwdOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="restPwdRules" label-width="80px">

        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.password" placeholder="请输入新密码" type="password" show-password/>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" placeholder="请确认新密码" type="password" show-password/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="restPwdInfo">保存</el-button>
          <el-button type="danger" size="mini" @click="close">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent,restPwd } from "@/api/edu/student";

export default {
  name: "Student",
  dicts: ['sys_user_sex','sys_have_not'],
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.form.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      restPwdOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        birthday: null,
        gender: null,
        guardian: null,
        guardianPhone: null,
        tag: null,
        avatar: null,
        tenantId: null,
        tenantName: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        guardian: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        guardianPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        studyTarget: [
          { required: true, message: "学习目标不能为空", trigger: "blur" }
        ],
      },
      restPwdRules:{
        password: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  // watch: {
  //   'form.foodAllergiesFlag'(newVal, oldVal) {
  //     this.form.isFoodAllergies = 'yes' == newVal
  //   },
  //   'form.diseaseFlag'(newVal, oldVal) {
  //     this.form.isDisease = 'yes' == newVal
  //   },
  //
  // },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    close(){
      this.restPwdOpen = false;
      this.form.password = null;
      this.form.confirmPassword = null;
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        id: null,
        birthday: null,
        gender: null,
        guardian: null,
        guardianPhone: null,
        // isFoodAllergies: false,
        // foodAllergiesFlag: null,
        // diseaseFlag: null,
        // foodAllergens: null,
        // isDisease: false,
        // diseaseName: null,
        tag: null,
        studyTarget: null,
        avatar: null,
        remark: null,
        tenantId: null,
        tenantName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        startTime: null,
        endTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        // this.form.foodAllergiesFlag = this.isFoodAllergies ? 'yes' : 'no';
        // this.form.diseaseFlag = this.isDisease ? 'yes' : 'no';
        this.open = true;
        this.title = "修改学生";
      });
    },
    handleRestPwd(row){
      this.restPwdOpen = true;
      this.form.id = row.id;
    },
    handleWrongQuestionBook(row){
      this.$router.push('/wrongQuestionBank/index/' + row.id)
    },
    restPwdInfo(){
      restPwd(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.restPwdOpen = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edu/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
